Completed
Pull Request — master (#49)
by Kyungmi
03:09
created

Status.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 10
Bugs 0 Features 0
Metric Value
cc 1
c 10
b 0
f 0
nc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
nop 0
1
 /**
2
 * Status model
3
 *
4
 * @since 1.0.0
5
 */
6
7 4
const Model = require('./Model');
8 4
const semver = require('semver');
9 4
const Cache = require('node-cache');
10 4
const NotifierError = require('../common/Error');
11
12 4
const cache = new Cache({ stdTTL: 60, checkperiod: 30 });
13
14
class Status extends Model {
15
  constructor() {
16 4
    super('status', [{ key: { isActivated: -1, startTime: 1, endTime: 1, createTime: 1 } }]);
17
  }
18
19
  find(query, sort, skip, limit) {
20 6
    return super.find(query, sort, skip, limit);
21
  }
22
23
  findWithCache(query, sort = {}) {
24 4
    return Promise.resolve().then(() => {
25 4
      const list = cache.get('status');
26 4
      if (list === undefined) {
27 2
        return this.find(query, sort).then(((result) => {
28 2
          cache.set('status', result);
29 2
          return result;
30
        }));
31
      }
32 2
      return list;
33
    });
34
  }
35
36
  findWithComparators(deviceType, deviceVersion, appVersion, sort) {
37 5
    const self = this;
38 5
    const now = new Date();
39 5
    if (deviceVersion !== '*' && !semver.valid(deviceVersion)) {
40 1
      return Promise.reject(new NotifierError(NotifierError.Types.BAD_REQUEST_INVALID,
41
        { message: `"${deviceVersion}"은 잘못된 버전 형식입니다.` }));
42
    }
43 4
    if (appVersion !== '*' && !semver.valid(appVersion)) {
44
      return Promise.reject(new NotifierError(NotifierError.Types.BAD_REQUEST_INVALID,
45
        { message: `"${appVersion}"은 잘못된 버전 형식입니다.` }));
46
    }
47 4
    return this.findWithCache({
48
      $or: [
49
        { startTime: { $lte: now }, endTime: { $gt: now }, isActivated: true },
50
        { startTime: { $exists: false }, endTime: { $exists: false }, isActivated: true },
51
      ],
52 4
    }, sort).then(results => results.filter(
53 16
      result => (deviceType === '*' || result.deviceTypes.includes(deviceType))
54
        && self._isSatisfiedVersion(result.deviceSemVersion, deviceVersion)
55
        && self._isSatisfiedVersion(result.appSemVersion, appVersion)
56
    ));
57
  }
58
59
  add(model) {
60 13
    const now = new Date();
61 13
    return super.add(Object.assign({}, model, { createTime: now, updateTime: now })).then((result) => {
62 13
      cache.del('status');
63 13
      return result;
64
    });
65
  }
66
67
  update(id, model, unset) {
68 4
    return super.update(id, Object.assign({}, model, { updateTime: new Date() }), unset).then((result) => {
69 4
      cache.del('status');
70 4
      return result;
71
    });
72
  }
73
74
  remove(id) {
75 13
    return super.remove(id).then((result) => {
76 13
      cache.del('status');
77 13
      return result;
78
    });
79
  }
80
81
  _isSatisfiedVersion(conditions, versionToCompare) {
82 27
    if (!conditions || versionToCompare === '*') {
83 24
      return true;
84
    }
85 3
    return semver.satisfies(versionToCompare, conditions);
86
  }
87
}
88
89
module.exports = new Status();
90